
<script type="text/javascript" charset="utf-8">

		var computerroom_id;
		var hardware_manufacturer;
		var hardware_name;
		var hardwarestatus_id;
		var rackspace_id;
		$(function(){
			
			computerroom_id = $('#computerroom_id').combobox({
				url:'/idcpost/computerroominfo',
				valueField:'id',
				textField:'cr_name',
				editable:false,
				panelHeight:'150',
				required:true,
				value:'{{=computerroom_id}}',
				onSelect:function(record){
					rackspace_id.combobox({
						url:'/idcpost/rackspaceinfo?id='+record.id,
						valueField:'id',
						textField:'rackspace_name',
						editable:false,
						panelHeight:'150',
						required:true
					}).combobox('clear');
				}
			})
			
			hardware_manufacturer = $('#hardware_manufacturer').combobox({
				url:'/devicespost/hardwaremanufacturerinfo',
				valueField:'id',
				textField:'hardwaremanufacturer_name',
				editable:false,
				panelHeight:'150',
				required:true,
				value:'{{=hardware_manufacturer}}',
				onSelect:function(record){
					hardware_name.combobox({
						url:'/devicespost/hardwareinfo?id='+record.id,
						valueField:'id',
						textField:'hardware_name',
						editable:false,
						panelHeight:'150',
						required:true,
					}).combobox('clear');
					//rackspace_id.combobox().combobox('clear');
				}
			});

			hardware_name = $('#hardware_name').combobox({
				url:'/devicespost/hardwareinfo?id={{=hardware_manufacturer}}',
				valueField:'id',
				textField:'hardware_name',
				editable:false,
				required:true,
				value:'{{=hardware_id}}'
			});
			
			rackspace_id = $('#rackspace_id').combobox({
				url:'/idcpost/rackspaceinfo?id={{=computerroom_id}}',
				valueField:'id',
				textField:'rackspace_name',
				editable:false,
				required:true,
				value:'{{=rackspace_id}}'
			});
			
			//状态信息
			hardwarestatus_id = $('#hardwarestatus_id').combobox({
				url:'/devicespost/hardwarestatusinfo',
				valueField:'id',
				textField:'hardwarestatus_name',
				editable:false,
				required:true,
				panelHeight:'150',
				value:'{{=hardwarestatus_id}}'
			});
			
			//easyui鼠标悬停提示
			$('#switch_ip_vlan').tooltip({
				position: 'right',
				content: '<span style="color:#fff">输入VLAN编号</span>',
				onShow: function(){
					$(this).tooltip('tip').css({
						backgroundColor: '#666',
						borderColor: '#666'
					});
				}
			});
		});
		
		//产生随机数
		function RndNum(n){
			var rnd="";
			for(var i=0;i<n;i++)
				rnd+=Math.floor(Math.random()*10);
			return rnd;
		}
		
		//添加行
		function addRowByID(currentRowID){
			var newid = RndNum(6); 
		   //遍历每一行，找到指定id的行的位置i,然后在该行后添加新行
			   $.each( $('#tb:first tbody tr'), function(i, tr){
				   //$("tr").each(function(i, tr){
				   if($(this).attr('id')==currentRowID){
					   //获取当前行
					   var currentRow=$('#tb:first tbody tr:eq('+i+')');
					   //var currentRow=$('tr:eq('+i+')');
					   //要添加的行的id
					   var addRowID=currentRowID+newid;
					   var switch_ip_vlan="switch_ip_vlan" + newid;
					   var switch_ip_addr="switch_ip_addr" + newid;
						str = "<tr id='" +addRowID + "'><th style='width:80px;'>IP Vlan配置</th>" +
								"<td><input type='hidden' name='sp' value='"+ newid +"' /><input id='" + switch_ip_vlan+"' name='" + switch_ip_vlan+
								"' class='easyui-numberbox' data-options=\"required:'true'\" style='width: 100px;' />" +            
                				" <input id='" + switch_ip_addr + "' name='" + switch_ip_addr+ "'" + 
								" class='easyui-validatebox' data-options=\"validType:'ip'\" style='width: 300px;' />" +
								"<input type='button' name='button' value='删除' onclick='delRowByID(this);' style='width: 50px;'>" +
								"</td></tr>";
					   //当前行之后插入一行
					   currentRow.after(str);
					   $('#'+switch_ip_vlan).tooltip({
							position: 'right',
							content: '<span style="color:#fff">输入VLAN编号</span>',
							onShow: function(){
								$(this).tooltip('tip').css({
									backgroundColor: '#666',
									borderColor: '#666'
								});
							}
						});
					   //保证easyui验证可以使用
						$.parser.parse('#'+addRowID);  //解析某个具体节点 
					   //$.parser.parse(); // 解析整个页面
					   return false; //停止循环
				   }
			   });
			 
	      }	 
	   
       
	   
   		//删除行
		function delRowByID(clickTd){
			var tr = $(clickTd).parent().parent();
			tr.remove();
		}

</script>

<div style="padding: 5px;">
	<form method="post">
    <input type="hidden" name="id" />
		<table class="tableForm" id="tb">
            <tr>
				<th style="width: 80px;">设备型号</th>
				<td>
               		<input style="line-height:26px;border:1px solid #ccc;width:130px" class="easyui-combobox" 
                        name="hardware_manufacturer" id="hardware_manufacturer" />                    
                    <input style="line-height:26px;border:1px solid #ccc;width:140px" class="easyui-combobox" 
                        name="hardware_name" id="hardware_name" />
				</td>
			</tr>
            <tr>
				<th style="width: 80px;">别名</th>
				<td><input name="switch_aliasname" class="easyui-validatebox" data-options="required:'true'" style="width: 200px;" />
				</td>
			</tr>
			<tr>
                  <th style="width:80px;">机房机柜</th>
                  <td>
					<input style="line-height:26px;border:1px solid #ccc;width:130px" class="easyui-combobox" 
                        name="computerroom_id" id="computerroom_id" />
					 <input style="line-height:26px;border:1px solid #ccc;width:130px" class="easyui-combobox" 
					  name="rackspace_id" id="rackspace_id" /> 						
                  </td>
			</tr>
            <tr>
				<th style="width: 80px;">序列号</th>
				<td><input name="switch_sn" class="easyui-validatebox" data-options="required:'true'" style="width: 400px;" />
				</td>
			</tr>
            <tr>
				<th style="width: 80px;">资产编号</th>
				<td><input name="switch_asset_tag" class="easyui-validatebox" data-options="required:'true'" style="width: 400px;" />
				</td>
			</tr>
			<tr>
                  <th style="width:80px;">状态信息</th>
                  <td>
                  <input style="line-height:26px;border:1px solid #ccc;width:130px" class="easyui-combobox" 
                        name="hardwarestatus_id" id="hardwarestatus_id" />
                  </td>
			</tr>
            {{if sw_ip_num==0:}}
            <tr id="tr_switch_ip">
				<th style="width: 80px;">IP Vlan配置</th>
				<td><input id="switch_ip_vlan" name="switch_ip_vlan" class="easyui-numberbox" style="width: 100px;" /> <input id="switch_ip_addr" name="switch_ip_addr" class="easyui-validatebox" data-options="validType:'ip'" style="width: 300px;" /><input type="button" name="button" value="添加" onclick="addRowByID('tr_switch_ip');" style="width: 50px;">
				</td>
			</tr>
            {{else:
            	count = 1
            		for i in sw_ip:
                    	if count == 1:
            }}
            <tr id="tr_switch_ip">
				<th style="width: 80px;">IP Vlan配置</th>
				<td><input id="switch_ip_vlan" name="switch_ip_vlan" value="{{=i.switch_ip_vlan}}" class="easyui-numberbox" style="width: 100px;" /> <input id="switch_ip_addr" name="switch_ip_addr" value="{{=i.switch_ip_addr}}" class="easyui-validatebox" data-options="validType:'ip'" style="width: 300px;" /><input type="button" name="button" value="添加" onclick="addRowByID('tr_switch_ip');" style="width: 50px;">
				</td>
			</tr>
            {{		else:}}
            <tr id="tr_switch_ip{{=random_num[count-2]}}">
				<th style="width: 80px;">IP Vlan配置</th>
				<td><input type='hidden' name='sp' value='{{=random_num[count-2]}}' /><input id="switch_ip_vlan{{=random_num[count-2]}}" name="switch_ip_vlan{{=random_num[count-2]}}" value="{{=i.switch_ip_vlan}}" class="easyui-numberbox" style="width: 100px;" /> <input id="switch_ip_addr{{=random_num[count-2]}}" name="switch_ip_addr{{=random_num[count-2]}}" value="{{=i.switch_ip_addr}}" class="easyui-validatebox" data-options="validType:'ip'" style="width: 300px;" /><input type="button" name="button" value="删除" onclick="delRowByID(this);" style="width: 50px;">
				</td>
			</tr>
            {{pass}}
            {{count = count + 1
            pass}}
            {{pass}}
            <tr>
				<th style="width: 80px;">备注</th>
				<td colspan="3"><textarea name="comment" style="height: 50px;width: 97%;"></textarea>
				</td>
			</tr>          
		</table>
	</form>
</div>